package com.javacv.plus.examples.exception;

import com.javacv.plus.core.exception.ProcessorException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.multipart.MaxUploadSizeExceededException;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(ProcessorException.class)
    public ResponseEntity<Map<String, String>> handleProcessorException(ProcessorException e) {
        log.error("处理失败", e);
        Map<String, String> response = new HashMap<>();
        response.put("error", "处理失败");
        response.put("message", e.getMessage());
        return new ResponseEntity<>(response, HttpStatus.INTERNAL_SERVER_ERROR);
    }

    @ExceptionHandler(MaxUploadSizeExceededException.class)
    public ResponseEntity<Map<String, String>> handleMaxUploadSizeExceededException(MaxUploadSizeExceededException e) {
        log.error("文件大小超出限制", e);
        Map<String, String> response = new HashMap<>();
        response.put("error", "文件大小超出限制");
        response.put("message", "上传的文件大小超过了允许的最大值");
        return new ResponseEntity<>(response, HttpStatus.PAYLOAD_TOO_LARGE);
    }

    @ExceptionHandler(UnsupportedOperationException.class)
    public ResponseEntity<Map<String, String>> handleUnsupportedOperationException(UnsupportedOperationException e) {
        log.error("不支持的操作", e);
        Map<String, String> response = new HashMap<>();
        response.put("error", "不支持的操作");
        response.put("message", e.getMessage());
        return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
    }

    @ExceptionHandler(Exception.class)
    public ResponseEntity<Map<String, String>> handleException(Exception e) {
        log.error("系统错误", e);
        Map<String, String> response = new HashMap<>();
        response.put("error", "系统错误");
        response.put("message", "服务器内部错误，请稍后重试");
        return new ResponseEntity<>(response, HttpStatus.INTERNAL_SERVER_ERROR);
    }
} 